package com.jy.controller;


import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.good.param.AddCartParam;
import com.jy.common.model.good.param.AddSearchGoodsHistoryParam;
import com.jy.common.tool.CommonTool;
import com.jy.service.GoodsService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author fzg
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 获取商品首页其他所有商品展示的信息
     * @return ResponseWrapper
     */
    @GetMapping("/getGoodsIndexRecommendInfo")
    public ResponseWrapper getGoodsIndexRecommendInfo(@RequestParam String type){
        return goodsService.getGoodsIndexRecommendInfo(type);
    }

    /**
     * 商品详情页面获取商品的全部信息
     * @return ResponseWrapper
     */
    @GetMapping("/getGoodsDetailInfo")
    public ResponseWrapper getGoodsDetailInfo(HttpServletRequest request, @RequestParam String goodAid){
        return goodsService.getGoodsDetailInfo(CommonTool.getUserAidByToken(request),Long.valueOf(goodAid));
    }


    /**
     * 选择完毕之后请求后端返回该样式的价格，积分，库存等
     * @return ResponseWrapper
     */
    @GetMapping("/getThisStyleInfoApi")
    public ResponseWrapper getThisStyleInfoApi(@RequestParam String style, @RequestParam Integer goodAid){
        return goodsService.getThisStyleInfoApi(style,goodAid);
    }


    /**
     *  用户取消收藏商品
     * @return ResponseWrapper
     */
    @GetMapping("/cancelCollectGoods")
    public ResponseWrapper cancelCollectGoods(HttpServletRequest request, @RequestParam Integer goodAid){
        return goodsService.cancelCollectGoods(CommonTool.getUserAidByToken(request),goodAid);
    }

    /**
     *  用户收藏商品
     * @return ResponseWrapper
     */
    @GetMapping("/collectGoods")
    public ResponseWrapper collectGoods(HttpServletRequest request, @RequestParam Integer goodAid){
        return goodsService.collectGoods(CommonTool.getUserAidByToken(request),goodAid);
    }

    /**
     *  获取当前样式aid
     * @return ResponseWrapper
     */
    @GetMapping("/getCurrentStyleAid")
    public ResponseWrapper getCurrentStyleAid(@RequestParam Integer goodAid, @RequestParam String currentStyleName){
//        System.out.println("样式：" + currentStyleName);
        return goodsService.getCurrentStyleAid(goodAid,currentStyleName);
    }


    /**
     *  加入购物车
     * @return ResponseWrapper
     */
    @PostMapping("/add-cart")
    public ResponseWrapper addCartApi(HttpServletRequest request, @RequestBody AddCartParam param){
        return goodsService.addCartApi(CommonTool.getUserAidByToken(request),param);
    }


    /**
     *  根据搜索关键词搜索商品
     * @return ResponseWrapper
     */
    @GetMapping("/getSearchResults")
    public ResponseWrapper getSearchResults(@RequestParam String searchKey, @RequestParam Integer selectedMenuAid){
        return goodsService.getSearchResults(searchKey,selectedMenuAid);
    }

    /**
     *  获取搜索历史和猜你想搜
     * @return ResponseWrapper
     */
    @GetMapping("/getsearchHistAndGuessSearchList")
    public ResponseWrapper getsearchHistAndGuessSearchList(HttpServletRequest request){
        return goodsService.getsearchHistAndGuessSearchList(CommonTool.getUserAidByToken(request));
    }


    /**
     *  点击搜索历史其中一个的清除图标删除那一个搜索历史
     * @return ResponseWrapper
     */
    @GetMapping("/deleteSearchByAid")
    public ResponseWrapper deleteSearchByAid(HttpServletRequest request,@RequestParam Integer aid){
        return goodsService.deleteSearchByAid(CommonTool.getUserAidByToken(request),aid);
    }


    /**
     *  添加搜索历史
     * @return ResponseWrapper
     */
    @PostMapping("/addSearchHistory")
    public ResponseWrapper addSearchHistory(HttpServletRequest request,@RequestBody AddSearchGoodsHistoryParam param){
        return goodsService.addSearchHistory(CommonTool.getUserAidByToken(request),param);
    }

    /**
     *  删除所有商品搜索记录
     * @return ResponseWrapper
     */
    @GetMapping("/deleteAllSearchHistory")
    public ResponseWrapper deleteAllSearchHistory(HttpServletRequest request){
        return goodsService.deleteAllSearchHistory(CommonTool.getUserAidByToken(request));
    }

}
